/**
 * Created by Yu Jin Xian on 2016/11/30.
 */
var bodyParser = require('body-parser');
var express = require('express');
var app = express();

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

// 示例9 基本表单处理
app.post('/process-contact-simpleness', function (req, res) {
    console.log('Received contact from ' + req.body.name + '<' + req.body.email + '>');
    // 保存到数据库...
    res.redirect(303, '/thank-you');
});

// 示例10 更强大的表单处理
app.post('/process-contact', function (req, res) {
    console.log('Received contact from ' + req.body.name + '<' + req.body.email + '>');
    try {
        // 保存到数据库...
        return res.xhr ?
            res.render({success: true}) :
            res.redirect(303, '/thank-you');
    } catch (e) {
        return res.xhr ?
            res.json({error: 'Database error'}) :
            res.redirect(303, '/database-error');
    }
});

app.listen(3000, function () {
    console.log('http://localhost:3000');
});